import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../components/Login.vue')
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../components/Home.vue'),
    redirect: '/welcome',
    children: [{
        path: '/welcome',
        name: 'welcome',
        component: () => import('../components/Welcome.vue')
      },
      // 用户
      {
        path: '/user',
        name: 'user',
        component: () => import('../components/User/User.vue')
      },
      {
        path: '/member',
        name: 'member',
        component: () => import('../components/User/Member.vue')
      },
      // 工人
      {
        path: '/worker',
        name: 'worker',
        component: () => import('../components/Worker/Worker.vue')
      },
      {
        path: '/worker/detail',
        name: 'worker_detail',
        component: () => import('../components/Worker/WorkerDetail.vue')
      },
      // 文章
      {
        path: '/article',
        name: 'article',
        component: () => import('../components/Article/Article.vue')
      },
      {
        path: '/article/write_article',
        name: 'write_article',
        component: () => import('../components/Article/WriteArticle.vue')
      },
      // 装修百科
      {
        path: '/adorn_pedia',
        name: 'adorn_pedia',
        component: () => import('../components/Article/AdornPedia.vue')
      },
      {
        path: '/adorn_pedia/detail',
        name: 'adorn_pedia_detail',
        component: () => import('../components/Article/AdornPediaDetail.vue')
      },
      // 装修流程
      {
        path: '/adorn_process',
        name: 'adorn_process',
        component: () => import('../components/Article/AdornProcess.vue')
      },
      // 关联产品或者文章
      {
        path: '/associated',
        name: 'associated',
        component: () => import('../components/Associated/Associated.vue')
      },
      // 系统
      {
        path: '/option',
        name: 'option',
        component: () => import('../components/System/Option.vue')
      },
      {
        path: '/coupons',
        name: 'coupons',
        component: () => import('../components/System/Coupons.vue')
      },
      // 服务
      {
        path: '/service',
        name: 'service',
        component: () => import('../components/Service/Service.vue')
      },
      // 案例
      {
        path: '/house',
        name: 'house',
        component: () => import('../components/ProjectCase/House.vue')
      },
      {
        path: '/house/detail',
        name: 'house_detail',
        component: () => import('../components/ProjectCase/HouseDetail.vue')
      },
      {
        path: '/project_case/:house_id',
        name: 'project_case_room',
        props: true,
        component: () => import('../components/ProjectCase/Room.vue')
      },
      // 产品
      {
        path: '/product',
        name: 'product',
        component: () => import('../components/Product/Product.vue')
      },
      {
        path: '/product/detail/',
        name: 'product_detail',
        props: true,
        component: () => import('../components/Product/ProductDetail.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂在路由导航守卫，及如果用户没有登录，则直接跳转到登录界面
router.beforeEach((to, from, next) => {
  /**
   * to 将要访问的路径
   * from 代表从哪个路径跳转而来
   * next 是一个函数 表示放行  next():表示放行  next('/login'):强制跳转
   */
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
